//
//  commodityDisplayViewController.m
//  代码布局
//
//  Created by iOS on 16/7/19.
//  Copyright © 2016年 iOS. All rights reserved.
//

#import "commodityDisplayViewController.h"
#import "productProfileTableViewCell.h"
#import "loopImageView.h"
#import "trastionAnimation.h"
#import "secondViewController.h"
@interface commodityDisplayViewController ()<UITableViewDataSource,UITableViewDelegate,loopImageViewFooterDelegate,ProduceProfileOfferTipLinkDelegate,UIViewControllerTransitioningDelegate,trastionAnimationDelegate>
//住表
@property(nonatomic,strong)UITableView * mainTableView;
@property(nonatomic,strong)NSArray * netDataArray;
//轮播
@property(nonatomic,strong)UIView  * loopIV;
@property(nonatomic,strong)UIView  * loopBackView;
@property (nonatomic,strong)UILabel * RoundView;
@property (nonatomic,strong)NSMutableArray * cellHeightArray;
@property (nonatomic,strong)trastionAnimation * trastion;
@end
static CGFloat loopimageHeight = 300;
@implementation commodityDisplayViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.title = @"commodity";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _cellHeightArray = [[NSMutableArray alloc]init];
    for (NSInteger i = 0; i< 100; i++) {
        [_cellHeightArray addObject:@(200)];
    }
    [self createMainTableView];
    [self loopImageview];
 //   [[NSUserDefaults standardUserDefaults] setObject:@"oooo" forKey:@"key"];
}

- (void)createMainTableView{
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.mainTableView.delegate =self;
    self.mainTableView.dataSource =self;
    self.mainTableView.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:self.mainTableView];
    [self.mainTableView registerClass:[productProfileTableViewCell class] forCellReuseIdentifier:@"productProfileTableViewCell"];
    [self.mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 30;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        productProfileTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"productProfileTableViewCell" forIndexPath:indexPath];
cell.basicInfoString = @"华硕（ASUS）灵耀 U3000 13.3英寸超轻薄笔记本电脑  (i7-6500U 8G 512GB SSD 烟棕色 LED雾面防炫目屏)";
        
        cell.OfferTipsString = @"抢券立减 满4000减400】【抢券立减 满4000减400】第1台笔记本没买think，第2台第3台就会体会think的经典！so~直接全垒打！点此领券实惠买小黑》》";
        cell.NetOfferTipsString = @"so~直接全垒打！点此领券实惠买小黑》》";
        
        cell.urlOfferTips = @"http://www.baidu.com";
        cell.productPrice = @"¥2000.00";
        cell.productOriginPrice = @"3999.00";
        cell.delegate =self;
        cell.exclusiveMobilePhoneString = @"300";
        cell.countDownTime = @"1480607248";
        cell.whiteIou = @"白条分期首次使用24期首次使用24期首次使用24期首次使用24期首次使用24期免利息分期";
        
        cell.promotionDict = @{@"限购":@"购买一件时享受优惠,超出数量已结算价格为准,购买一件时享受优惠,超出数量",@"加价购":@"满999另加100元,或满1999另加500块,即可兑换高级口香糖一颗另外还送板栗一个",@"优惠套装":@"套装套装"};
      //  cell.promotionDict = nil;
        [_cellHeightArray replaceObjectAtIndex:indexPath.row withObject:@([cell fitSizeHeight])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
         UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
    }
}
- (void)offerTipLink:(NSString *)url andtitle:(NSString *)title{
    NSLog(@"url = %@  标题 = %@",url,title);
}
- (void)loopImageview{
     NSArray * array = @[@"IMG_0740.JPG",@"IMG_0741.JPG",@"IMG_0742.JPG",@"IMG_0743.JPG",@"IMG_0744.JPG",];
    self.netDataArray = array;
    UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",array[0]]];
    loopimageHeight =  (WIDTH/image.size.width)*image.size.height;
    self.loopBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, loopimageHeight)];
    self.mainTableView.tableHeaderView = self.loopBackView;
    
     [self.mainTableView insertSubview:self.loopBackView atIndex:0];
   
    loopImageView * loopvc = [[loopImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, loopimageHeight) AnddataArray:array];
    self.loopIV = loopvc;
    loopvc.delegate =self;
    [self.loopBackView addSubview:loopvc];
   // [self roundViewForCurrentImage];
    [self performSelector:@selector(roundViewForCurrentImage) withObject:self afterDelay:0.2];
}

- (void)loopImageViewFooterTranster:(CGFloat)angle AndIndexPath:(NSInteger)indexpath AndTotal:(NSInteger)total{
  //http://www.itstrike.cn/Question/dbf931f6-2788-4689-8a18-1a9b88738380.html
    [self.view insertSubview:self.RoundView atIndex:self.RoundView.subviews.count - 2];
 
  
    if (angle > - 0.5) {
        [self.RoundView.layer setTransform:[self roundViewTransformAngel:angle]];
        self.RoundView.layer.zPosition = WIDTH/20.0;
    }else{
        [self.RoundView.layer setTransform:[self roundViewTransformAngel: 1-angle]];
        self.RoundView.layer.zPosition = WIDTH/20.0;
 
    }

    
    
    NSString * str1 = [NSString stringWithFormat:@"%ld/%ld",indexpath+1, total];
    NSMutableAttributedString * AttributedStr = [[NSMutableAttributedString alloc]initWithString:str1];
    NSRange  rang = [str1 rangeOfString:[NSString stringWithFormat:@"%ld",indexpath + 1]];
    NSRange rang2 = [str1 rangeOfString:[NSString stringWithFormat:@"/%ld",total]];
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:22.0]
     
                          range:rang];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:16.0]
     
                          range:rang2];
       if(angle < -0.5){
        self.RoundView.attributedText = AttributedStr;

    }
   }
#pragma mark 创建圆形页脚
- (void)roundViewForCurrentImage{
    UILabel * roundView = [[UILabel alloc]init];
    roundView.backgroundColor = [UIColor lightGrayColor];
    roundView.alpha = 0.0;
        roundView.layer.masksToBounds = YES;
        roundView.layer.cornerRadius = WIDTH/20.0;
    self.RoundView = roundView;
    roundView.textAlignment = 1;
    roundView.textColor = [UIColor whiteColor];
    [self.loopIV addSubview:roundView];
    [roundView makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(WIDTH/10.0);
        make.height.equalTo(WIDTH/10.0);

        make.bottom.equalTo(self.loopIV.bottom).offset(-20);
        make.right.equalTo(self.loopIV.right).offset(-20);
    }];
    NSString * str1 = [NSString stringWithFormat:@"1/%ld",self.netDataArray.count];
    NSMutableAttributedString * AttributedStr = [[NSMutableAttributedString alloc]initWithString:str1];
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:22.0]
     
                          range:NSMakeRange(0, 1)];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:16.0]
     
                          range:NSMakeRange(1, str1.length - 1)];
    roundView.attributedText = AttributedStr;
    [UIView animateWithDuration:1.0 animations:^{
        self.RoundView.alpha = 0.3;
    }];
}
- (CATransform3D)roundViewTransformAngel:(CGFloat)angle{
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = 1.0/-900;
    transform = CATransform3DRotate(transform, -angle *M_PI, 0, 1, 0);
    return transform;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGRect frame = self.loopBackView.frame;
    frame.size.height = (scrollView.contentOffset.y) + loopimageHeight + 64;
    self.loopBackView.frame = frame;
     self.loopIV.center = self.loopBackView.center;
    [self.mainTableView insertSubview:self.loopBackView atIndex:0];


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  
    secondViewController * sec = [[secondViewController alloc]init];
    sec.transitioningDelegate = self;
    [self presentViewController:sec animated:YES completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [_cellHeightArray[indexPath.row] floatValue];
}

- (trastionAnimation*)trastion{
    if (!_trastion) {
        _trastion = [[trastionAnimation alloc]init];
        _trastion.delegation = self;
    }
    return _trastion;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    self.trastion.reverse = NO;//跳转
    return self.trastion;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    self.trastion.reverse = YES;
    return self.trastion;
}
- (CGRect)getframeToView:(UIView *)view{
    return [self selfFrameRectFromeCurrentMainscreen:view];
}

- (CGRect)selfFrameRectFromeCurrentMainscreen:(UIView*)viewc{
    UIViewController * subVC = [self parentController];
    CGRect point = [[viewc superview] convertRect:viewc.frame toView:subVC.view];
    return point;
}
- (UIViewController *)parentController
{
    UIResponder *responder = [self nextResponder];
    while (responder) {
        if ([responder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)responder;
        }
        responder = [responder nextResponder];
    }
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



@end
